package core;

import java.awt.*;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.*;
import javax.swing.event.CaretEvent;
import javax.swing.event.CaretListener;

public class Tester {
	
	public static String parseFormatting(String str) {
		
		boolean inI = false, inB = false, inU = false;
		StringBuilder sb = new StringBuilder();
		char c, last=' ';
		
		for (int i=0; i<str.length(); i++) {
			c = str.charAt(i);
			
			if ("*/_".indexOf(c) != -1) {
				if (c == last) {
					sb.append(c);
					last=' ';
					continue;
				}
			}
			else {
				switch (last) {
				case '*':
					if (inB) {
						sb.append("</b>");
						inB = false;
					}
					else {
						sb.append("<b>");
						inB = true;
					}
					break;
				case '/':
					if (inI) {
						sb.append("</i>");
						inI = false;
					}
					else {
						sb.append("<i>");
						inI = true;
					}
					break;
				case '_':
					if (inU) {
						sb.append("</u>");
						inU = false;
					}
					else {
						sb.append("<u>");
						inU = true;
					}
					break;
				}
				sb.append(c);
			}
			last = c;
		}
		
		return sb.toString();
	}
	
	public static void main (String[] args) { //throws java.io.IOException {
/*		File file = new File("data");
		File file2 = new File(file, "songs");
		System.out.println(file2.getAbsolutePath());*/
		
//		 Determine which fonts support Chinese here ...
		
		/*Font[] allfonts = GraphicsEnvironment.getLocalGraphicsEnvironment().getAllFonts();
		int fontcount = 0;
		String chinesesample = "\u4e00";
		for (int j = 0; j < allfonts.length; j++) {
		    if (allfonts[j].canDisplayUpTo(chinesesample) == -1) { 
		        System.out.println(allfonts[j].getFontName());
		    }
	  	    fontcount++;
		}
		System.out.println("Found " + fontcount + " Chinese fonts");*/
		
		//System.out.println(new SimpleDateFormat("yyyy.MM.dd").format(new Date()));
		
		/*JFrame fr = new JFrame();
		fr.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		JScrollPane sp = new JScrollPane(new SongEditor());
		sp.setPreferredSize(new Dimension(300, 200));
		fr.getContentPane().add(sp);
		fr.pack();
		fr.setVisible(true);*/
		
		System.out.println(parseFormatting("Hi, my name is __*bob jones*__ and i'm from **/indiana/**." +
				" _nice to meet you_. where is the washroom//bathroom?"));
	}
}

class SongEditor extends JTextArea {
	
	private static final long serialVersionUID = 1L;
	
	public SongEditor() {
		super();
		setFont(new Font("Arial Unicode MS", Font.PLAIN, 13));

		
		addCaretListener(new CaretListener() {
			public void caretUpdate(CaretEvent e) {
				calcHighlightArea();
			}
		});
		addKeyListener(new KeyAdapter(){
			@Override
			public void keyPressed(KeyEvent e) {
				repaint();
			}
		});
		addMouseListener(new MouseAdapter() {
			@Override
			public void mousePressed(MouseEvent e) {
				new Thread() {
					@Override
					public void run() {
						int delay = getCaret().getBlinkRate() / 2;
						try {
							Thread.sleep(delay);
						} catch (InterruptedException e) {
							e.printStackTrace();
						}
						repaint();
					}
				}.start();
			}
			@Override
			public void mouseReleased(MouseEvent e) {
				mousePressed(e);
			}
		});
	}
	
	@Override
	public void setFont(Font f) {
		super.setFont(f);
		lineHeight = getFontMetrics(f).getHeight();
	}
	
	private void calcHighlightArea() {
		String txt = getText();
		int startPos, endPos,
			pos = getCaretPosition();
		
		
		for (startPos=pos-1; startPos>0 && startPos<txt.length(); startPos--) {
			//System.out.print(txt.charAt(startPos));
			if(txt.charAt(startPos) == '[') {
				break;
			}
		}
		
		// search forward until next tag or end of file is found
		for (endPos=pos; endPos>0 && endPos<txt.length(); endPos++)
			if(txt.charAt(endPos) == '[') {
				//hLines = countRows(txt, startPos+1, endPos);
				break;
			}
		// fix endPos if out of bounds
		if (endPos>0 && endPos == txt.length())
			endPos--;
		
		hFrom = countRows(txt, 0, startPos);
		hLines = countRows(txt, startPos+1, endPos) + 
				((endPos < txt.length() && txt.charAt(endPos)=='[') ? 0 : 1);
		
//		System.out.printf("pos %d / ", pos);
//		System.out.printf("start %d end %d / ", startPos, endPos);
//		System.out.printf("from %d height %d\n", hFrom, hLines);
		
	}
	// Counts rows between positions start and end in txt
	private static int countRows(String txt, int start, int end) {
		int row = 0;
		
		for (int i=start; i<end; i++) {
			if (txt.charAt(i) == '\n')
				row++;
		}
		return row;
	}
	
	private int hFrom,  hLines, lineHeight;
	
	@Override
	protected void paintComponent(Graphics g) {
		Rectangle oldClip = g.getClipBounds();
		//System.out.println("old clip: " + g.getClip());
		//g.setClip(0, 0, getWidth(), getHeight());
		
		// first clear the clipping area, since we're setting opaque to false
		g.clearRect(oldClip.x, oldClip.y, oldClip.width, oldClip.height);
		//g.clearRect(0, 0, getWidth(), getHeight());
		
		// set the clipping area to the entire textbox to allow us to draw the shading
		//g.setClip(new Rectangle(0, 0, getWidth(), getHeight()));
		//System.out.println("new clip: " + g.getClip());
		
		int start = lineHeight * hFrom,
			end = lineHeight * hLines;
		g.setColor(Color.lightGray);
		g.fillRect(0, start, getWidth(), end);
		
		// set the clipping area back to the original
		//g.setClip(oldClip);
		//g.setClip(new Rectangle(0, 0, getWidth(), getHeight()));
		
		// temporarily set opaque to prevent default paintComponent() 
		//  from clearing the highlighted region
		setOpaque(false);	
		super.paintComponent(g);
		setOpaque(true);
	}
}